﻿using UnityEngine;
using System.Collections;
using System;

/// 联想广告
public class LenovoAds : AdsBase
{
	public override void Init (params string[] key)
	{
		if (jo == null) {
			jo = new AndroidJavaObject ("com.lenovo.Main");
			jo.CallStatic ("setCallBack", "TJSDK", "OnADCallBack");
			jo.CallStatic ("init");
		}
		if (key.Length > 0) {
			jo.CallStatic ("initBanner", key [0],(int)BannerPos.down, (int)BannerwWidth.wrap);
		}
		if (key.Length > 1) {
			jo.CallStatic ("initInsert", key [1]);
		}
		if (key.Length > 2) {
			jo.CallStatic ("initIncent", key [2]);
		}

		LoadInsert ();
	}

	#region  展示 横幅 插屏 原生 开屏

	public override void ShowBanner (Action<bool> ac)
	{
		TJLog ("展示 Banner 广告");
		bannerCallBack = ac;
		jo.CallStatic ("showBanner");
	}

	public override void CloseBanner ()
	{
		TJLog ("关闭 Banner 广告");
		jo.CallStatic ("closeBanner");
	}

	public override void ShowInsert (Action<bool> ac)
	{
		TJLog ("展示插屏广告");
		insertCallBack = ac;
		jo.CallStatic ("showInsert");
	}

	public override void ShowIncent (Action<bool> ac)
	{
		TJLog ("展示开屏广告");
		incentCallBack = ac;
		jo.CallStatic ("showIncent");
	}

	#endregion



	#region 判断Banner和插屏是否加载好

	public override bool IsBannerReady ()
	{	
		TJLog ("检测Banner状态");
		bool state = jo.CallStatic<bool> ("isBannerReady");
		TJLog (state ? "Banner加载好啦" : "Banner没加载好");

		return state;
	}

	public override bool IsInsertReady ()
	{
		TJLog ("检测插频状态");
		bool state = jo.CallStatic<bool> ("isInsertReady");
		TJLog (state ? "插屏加载好啦" : "插屏没加载好");

		if (!state) {
			LoadInsert ();
		}

		return state;
	}

	public override bool IsIncentReady ()
	{
		TJLog ("检测激励视频状态");
		TJLog ("激励视频可用");
		return true;
	}

	#endregion

	public override void OnADCallback (string str)
	{
		TJLog ("广告回调: " + str);
		string[] datas = str.Split ('|');
		switch (datas [0]) {
		case AdsTypes.banner:
			switch (datas [1]) {
			case AdsStates.recive:				TJLog ("收到Banner广告");				break;
			case AdsStates.noAD:				TJLog ("无Banner广告");
				if (bannerCallBack != null) {
					bannerCallBack (false);
					bannerCallBack = null;
				}
				break;
			case AdsStates.exposure: 			TJLog ("曝光Banner广告");
				if (bannerCallBack != null) {
					bannerCallBack (true);
					bannerCallBack = null;
				}
				break;																
			case AdsStates.click: 				TJLog ("点击Banner广告"); 				break;
			case AdsStates.closed:				TJLog ("关闭Banner广告");				break;
			default:							TJLog ("其它Banner回调：" + datas [1]);	break;
			}
			break;
		case AdsTypes.insert:
			switch (datas [1]) {
			case AdsStates.recive:				TJLog ("收到插屏广告");					break;
			case AdsStates.noAD:				TJLog ("无插屏广告");	
				if (insertCallBack != null) {
					insertCallBack (false);
					insertCallBack = null;
				}
				break;
			case AdsStates.open:				TJLog ("打开插屏广告");
				if (insertCallBack != null) {
					insertCallBack (true);
					insertCallBack = null;
				}
				break;
			case AdsStates.exposure:			TJLog ("曝光插屏广告"); 					break;
			case AdsStates.click:				TJLog ("点击插屏广告");					break;
			case AdsStates.closed:				TJLog ("关闭插屏广告");					break;
			default:							TJLog ("其它插屏回调：" + datas [1]);		break;
			}
			break;
		case AdsTypes.incent:
			switch (datas [1]) {
			case AdsStates.recive:				TJLog ("收到激励视频");					break;
			case AdsStates.noAD:				TJLog ("无激励视频");
				if (incentCallBack != null) {
					incentCallBack (false);
					incentCallBack = null;
				}
				break;
			case AdsStates.exposure:			
				TJLog ("曝光激励视频");
				if (incentCallBack != null) {
					incentCallBack (true);
					incentCallBack = null;
				}
				break;
			case AdsStates.click:				TJLog ("点击激励视频");					break;
			case AdsStates.closed:				TJLog ("关闭激励视频");					break;
			default:							TJLog ("其它激励视频回调：" + datas [1]);		break;
			}
			break;
		default:
			break;
		}
	}
}
